<?php
// get custom post type ==> slides
global $post;
$args = array(
	'post_type' =>'slides',
	'numberposts' => -1,
	'orderby' => 'ASC'
);
$slider_posts = get_posts($args);
?>

<?php
// show slider only if slides exist
if($slider_posts) {
// add slider descriptions to array
$captions = array();
?>
<div id="homepage-slider-wrap">
<div id="homepage-slider-pattern">
    <div id="homepage-slider">
            <div id="slider" class="nivoSlider clearfix">
				<?php
                // start the loop
                foreach($slider_posts as $post) : setup_postdata($post);
                // get image
                $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'home-slide');
                // get metabox data
                $slidecaption = get_post_meta($post->ID, 'slides_description', TRUE);
                $slidelink = get_post_meta($post->ID, 'slides_url', TRUE);
                ?>
                <?php
                // check if the slide has a description
                if($slidecaption != '') { ?>
                <?php
                // save slide descriptions as array
                $captions[] = $slidecaption ?>
                <?php }?>
                <?php
                // show link with slide if meta exists
                if($slidelink != '') { ?>
                	<a href="<?php echo $slidelink ?>" title="<?php the_title(' ') ?>"><img src="<?php echo $thumbnail[0]; ?>" height="320" width="920" alt="<?php the_title(); ?>" <?php if($slidecaption != '') { ?> title="#caption<?php echo count($captions)-1; ?>"<?php } ?> /></a>
                <?php
                // no meta link defined, show plain img
                } else { ?>
                	<img src="<?php echo $thumbnail[0]; ?>" height="320" width="920" alt="<?php the_title(); ?>" <?php if($slidecaption != '') { ?> title="#caption<?php echo count($captions)-1; ?>"<?php } ?> />
                <?php } ?>
                <?php endforeach; ?>
                <?php wp_reset_postdata(); ?> 
                </div>
            	<!-- END slider -->  
            <?php
            foreach($captions as $key => $caption) :
            ?>
            <div id="caption<?php echo $key; ?>" class="nivo-html-caption">
            	<?php echo $caption; ?>
            </div>
            <!-- END caption -->
            <?php endforeach; } ?>
            <span class="slider-shadow"></span>
        </div>
    	<!-- END homepage-slider -->
        </div>
        <!-- END homepage-slider-pattern -->
</div>
<!-- END homepage-slider-wrap -->